/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.ImageDao;
import com.techshop.model.Image;
import java.util.List;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Hai-PC
 */
public class ImageDaoImpl extends HibernateDaoSupport implements ImageDao
{
    @Override
    public boolean insert(Image image)
    {
        try
        {
            getHibernateTemplate().save(image);
            return true;
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(Image image)
    {
        try
        {
            getHibernateTemplate().delete(getHibernateTemplate().merge(image));
            return true;
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public List<Image> getByPhone(int phoneId, String category)
    {
        try
        {
            return getSession().createQuery("FROM Image WHERE phone.id=:phoneId AND category=:category").setInteger("phoneId", phoneId).setString("category", category).list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<Image> getByPhoneAndColor(int phoneId, String colorId, String category)
    {
        try
        {
            return getSession().createQuery("FROM Image WHERE phone.id=:phoneId AND color.id=:colorId AND category=:category").setInteger("phoneId", phoneId).setString("colorId", colorId).setString("category", category).list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
}